#include "MainFrame/RIVIAMainFrame.h"

#include "MainFrame/CentralWidget/CentralWidget.h"
#include "MainFrame/ToolBar/Tools.h"
#include "MainFrame/StatusBar/StatusBar.h"
#include "MainFrame/MenuBar/MenuBar.h"
#include "MainFrame/SettingDock/ToolOptionsDock.h"
#include "MainFrame/SettingDock/BrushesDock.h"
#include "MainFrame/SettingDock/LayersDock.h"
#include "MainFrame/SettingDock/ChannelsDock.h"
#include "MainFrame/SettingDock/PaletteDock.h"

#include <QMouseEvent>
#include <QEvent>

RIVIAMainFrame::RIVIAMainFrame(QWidget *parent) :
    QMainWindow(parent),
    m_pCentralWidget(nullptr),
    m_pTools(nullptr),
    m_pStatusBar(nullptr),
    m_pMenuBar(nullptr),
    m_pToolOptionsDock(nullptr),
    m_pBrushesDock(nullptr),
    m_pLayersDock(nullptr),
    m_pChannelsDock(nullptr),
    m_pPaletteDock(nullptr)
{
    this->setWindowTitle(tr("RIVIA Image Manipulation Platform"));
    initCentralWidget();
    initToolBar();
    initStatusBar();
    initMenuBar();
    initSettingDock();

    connetSignalToSlots();
    this->setMinimumSize(1000, 600);
    this->showMaximized();
}

RIVIAMainFrame::~RIVIAMainFrame()
{

}

void RIVIAMainFrame::CloseMainWindow()
{
    this->close();
}

void RIVIAMainFrame::initCentralWidget()
{
    m_pCentralWidget = new CentralWidget(this);
    this->setCentralWidget(m_pCentralWidget);
    m_pCentralWidget->showMaximized();
}

void RIVIAMainFrame::initToolBar()
{
    m_pTools = new Tools(this);
    this->addToolBar(m_pTools);
}

void RIVIAMainFrame::initStatusBar()
{
    m_pStatusBar = new StatusBar(this);
    this->setStatusBar(m_pStatusBar);
}

void RIVIAMainFrame::initMenuBar()
{
    m_pMenuBar = new MenuBar(this);
    connect(m_pMenuBar, SIGNAL(closeMainFrame()), this, SLOT(CloseMainWindow()));

    this->setMenuBar(m_pMenuBar);
}

void RIVIAMainFrame::initSettingDock()
{
    m_pToolOptionsDock = new ToolOptionsDock("Tool Options", this);
    m_pToolOptionsDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    this->addDockWidget(Qt::LeftDockWidgetArea, m_pToolOptionsDock);

    m_pPaletteDock = new PaletteDock("Color Palette", this);
    m_pPaletteDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    this->addDockWidget(Qt::LeftDockWidgetArea, m_pPaletteDock);

    m_pBrushesDock = new BrushesDock("Brushes", this);
    m_pBrushesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    this->addDockWidget(Qt::RightDockWidgetArea, m_pBrushesDock);

    m_pLayersDock = new LayersDock("Layers", this);
    m_pLayersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    this->addDockWidget(Qt::RightDockWidgetArea, m_pLayersDock);

    m_pChannelsDock = new ChannelsDock("Channels", this);
    m_pChannelsDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    this->addDockWidget(Qt::RightDockWidgetArea, m_pChannelsDock);

    this->tabifyDockWidget(m_pLayersDock, m_pChannelsDock);
    m_pLayersDock->raise();
}

void RIVIAMainFrame::connetSignalToSlots()
{
    connect(m_pCentralWidget, SIGNAL(cursorPosChanged(QPoint&)),
            m_pStatusBar, SLOT(onCursorPosChanged(QPoint&)));
    connect(m_pCentralWidget, SIGNAL(imageAdded(int,QString)),
            m_pLayersDock, SLOT(addLayerDockWidget(int,QString)));
    connect(m_pCentralWidget, SIGNAL(imageDeleted(int)),
            m_pLayersDock, SLOT(delLayerDockWidget(int)));
    connect(m_pCentralWidget, SIGNAL(imageChanged(int)),
            m_pLayersDock, SLOT(changeLayerDockWidget(int)));
}

void RIVIAMainFrame::changeEvent(QEvent *event)
{
    QWidget::changeEvent(event);
    switch (event->type())
    {
    case QEvent::LanguageChange:
        initTrans();
        m_pPaletteDock->initTrans();
        break;
    default:
        break;
    }
}

void RIVIAMainFrame::initTrans()
{
    m_pToolOptionsDock->setWindowTitle(tr("Tool Options"));
    m_pPaletteDock->setWindowTitle(tr("Color Palette"));
    m_pBrushesDock->setWindowTitle(tr("Brushes"));
    m_pLayersDock->setWindowTitle(tr("Layers"));
    m_pChannelsDock->setWindowTitle(tr("Channels"));
    this->setWindowTitle(tr("RIVIA Image Manipulation Platform"));
}
